package com.imooc.springboot.study.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <h1>springboot 定时任务</h1>
 */
@Slf4j
@Component
public class BootSchedule {

    private final DateTimeFormatter fmt = DateTimeFormatter.ofPattern(
            "HH:mm;ss"
    );

    @Scheduled(fixedRate = 3000)
    public void schedule01() {
        log.info("schedule01->{}", LocalDateTime.now().format(fmt));
    }

    @Scheduled(fixedDelay = 3000)
    public void schedule02() {
        log.info("schedule01->{}", LocalDateTime.now().format(fmt));
    }

    /**
     * <h1>第一次延迟两秒后执行，之后按照每3秒执行一次</h1>
     */
    @Scheduled(initialDelay = 2000, fixedRate = 3000)
    public void schedule03() {
        log.info("schedule01->{}", LocalDateTime.now().format(fmt));
    }

    @Scheduled(cron = "*/3 * * * * ?")
    public void schedule04() {
        log.info("schedule01->{}", LocalDateTime.now().format(fmt));
    }
}
